using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace HLSSignature
{
    class SAFEImage
    {
        
        public enum enmSAFESignatureStatus
        {
            ValidateSuccess,
            ValidateFail,
            NotValidated,
        }

        public  const string SAFEDisclaimer = "I intend this signature to be legally binding per my executed SAFE user agreement";



        const int bigSize = 12;
            const int midSize = 10;
            const int smallSize = 8;

            //set drawing pen
            Pen blackPen = new Pen(Color.Black);
        Pen whitePen = new Pen(Color.White);
           
            //set font
            Font bigFont = new Font("Arial", bigSize);
            Font midFont = new Font("Arial", midSize);
            Font smallFont = new Font("Arial", smallSize);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
        SolidBrush whiteBrush = new SolidBrush(Color.White);
        private StringFormat drawFormat
        {
            get{ 
                // Set format of string.
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Near;
                return sf;
            }
        }

        
        public Bitmap CreateSignLine(string suggestedText,string suggestedSigner, int w, int h)
        {
            //we need minimal width and height
            if (w < 50) w = 50;
            if (h < 170) h = 170;

            System.Drawing.Bitmap draw = new System.Drawing.Bitmap(w, h);
            Graphics g = Graphics.FromImage(draw);
            g.DrawRectangle(whitePen, 0, 0, w, h);
            g.FillRectangle(whiteBrush, 2, 2, w - 4, h - 4);
            g.DrawImage(global::HLSSignature.Properties.Resources.signMark, new Point(10, 40));
            g.DrawLine( blackPen, new Point(80, 80), new Point(w - 4, 80));
            g.DrawString(SAFEDisclaimer, midFont, blackBrush, new RectangleF(20, 90, w -20, 50), drawFormat);
            g.DrawString(suggestedText, smallFont, blackBrush, new RectangleF(20, 140, w-20,20), drawFormat);
            g.DrawString(suggestedSigner, midFont, blackBrush, new RectangleF(20, 160, w-20, h-160), drawFormat);
            g.Dispose();
            return draw;
        }

        public Bitmap CreateSignLineSoftwareRequired(string requiredText, int w, int h)
        {
            System.Drawing.Bitmap draw = new System.Drawing.Bitmap(w, h);
            Graphics g = Graphics.FromImage(draw);
            g.DrawRectangle(whitePen, 0, 0, w, h);
            g.FillRectangle(whiteBrush, 2, 2, w - 4, h - 4);
            g.DrawString(requiredText, midFont, blackBrush, new PointF(20, 20));
            g.Dispose();
            return draw;
        }
        public Bitmap CreateSAFEImage(enmSAFESignatureStatus enmStatus, string signerName, string signReason, DateTime signDate, int w, int h)
        {


            //string SAFEDisclaimer = "I intend this signature to be legally binding per my executed SAFE user agreement";
            string reason = signReason;
            //divide the panel to 2 halves 
            //left half is for name and logo 
            //right half for reason, disclaimer, and date
            Image safeImage =  global::HLSSignature.Properties.Resources.safeLogo;

            Image validImage =  global::HLSSignature.Properties.Resources.question;

            switch (enmStatus)
            {
                //case enmSAFESignatureStatus.NotValidated:
                //    validImage = global::HLSSignature.Properties.Resources.question;
                //    break;
                case enmSAFESignatureStatus.ValidateFail:
                    validImage = global::HLSSignature.Properties.Resources.invalid;
                    break;
                case enmSAFESignatureStatus.ValidateSuccess:
                    validImage = global::HLSSignature.Properties.Resources.valid;
                    break;
                default:
                    break;
            }

            Bitmap bmp = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.White, new Rectangle(0, 0, w, h));

            //left half
            //left half of the left half is for name and whether it is valid

            //draw valid image on left half of the left half
            g.DrawImage(validImage,
                new RectangleF(w/16, h/4, w/8, h/3),
               new RectangleF(0, 0, validImage.Width, validImage.Height),
               GraphicsUnit.Pixel);


            // Draw string to screen at upper 1/3.
            g.DrawString(signerName, midFont, blackBrush, new Point(5, h/3));


            //now draw SAFE logo to right half of the panel
            //now draw SAFE logo to right half of the panel
            //scale the image if needed
            int drawW = w / 4 - 2;
            int drawH = h / 2;

            if (drawW < safeImage.Width)
            {
                //calculate scaling ratio based on width
                float r = (float)drawW / safeImage.Width;

                //calculate corresponding drawing height
                int temp = (int)(r * safeImage.Height);
                if (temp > drawH)  //need to scale based on height instead
                {
                    r = (float)drawH / safeImage.Height;
                    drawW = (int)(r * safeImage.Width);
                }
                else
                {
                    drawH = temp;
                }

            }
            else if (drawH < safeImage.Height)
            {
                //calculate scaling ratio based on height
                float r = (float)drawH / safeImage.Height;

                //calculate corresponding drawing height
                drawW = (int)(r * safeImage.Width);

            }


            g.DrawImage(safeImage,
                new RectangleF(w / 4 + 2, h / 2, drawW, drawH),
               new RectangleF(0, 0, safeImage.Width, safeImage.Height),
                GraphicsUnit.Pixel);
           
            //now draw reson and dates on right half of the panel
            //first make sure the string is not too long
            SizeF stringSize = new SizeF();
            stringSize = g.MeasureString(reason, midFont, w / 2);
            
            //drawFormat.Alignment = StringAlignment.Near;
            //drawFormat.LineAlignment = StringAlignment.Near;
            if (stringSize.Height > h * 0.75)
            {
                //use small font
                g.DrawString(reason, smallFont, blackBrush, new RectangleF(w / 2 + 4, 0, w / 2, h *3/4), drawFormat);
                stringSize = g.MeasureString(reason, smallFont, w / 2);
            }
            else
            {
                g.DrawString(reason, midFont, blackBrush, new RectangleF(w / 2 + 4, 0, w / 2, h*3/4), drawFormat);
            }


            g.DrawString("Date: " + signDate.ToString(), midFont, blackBrush, new RectangleF(w / 2 + 4, h * 3 / 4 + 1, w / 2, h / 4), drawFormat);

            g.Dispose();

            return bmp;

        }
    }
}
